@extends('layouts/index')

@section('title',$default_name)

@section('content')
    <div class="layui-card">
        <div class="layui-card-header notselect">
            <span class="layui-icon layui-icon-next font-s10 color-desc margin-right-5"></span> {{ $default_name }}
            <div class="pull-right">
                @if(array_key_exists('base.role.add',$btns))
                    <button class="layui-btn layui-btn-sm layui-btn-primary" onclick="addRole()">
                        {{ $btns['base.role.add'] }}
                    </button>
                @endif
                @if(array_key_exists('base.role.batch.del',$btns))
                    <button class="layui-btn layui-btn-sm layui-btn-primary" onclick="batchRemove()">
                        {{ $btns['base.role.batch.del'] }}
                    </button>
                @endif
            </div>
        </div>
        <div class="layui-card-body">
            <table class="layui-table">
                <caption class="margin-bottom-10 text-left">
                    <fieldset>
                        <form class="layui-form layui-form-pane form-search" id="roleForm" method="get"
                              autocomplete="off">
                            <div class="layui-form-item layui-inline">
                                <label class="layui-form-label">{{ trans("role.role_name") }}</label>
                                <div class="layui-input-inline">
                                    <input name="name" id="name" value="{{ isset($name) ? $name : "" }}"
                                           placeholder="{{ trans("role.please_input_role_name") }}"
                                           class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item layui-inline">
                                <label class="layui-form-label">{{ trans("role.role_desc") }}</label>
                                <div class="layui-input-inline">
                                    <input name="desc" id="desc" value="{{ isset($desc) ? $desc : "" }}"
                                           placeholder="{{ trans("role.please_input_role_desc") }}"
                                           class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item layui-inline">
                                <label class="layui-form-label">{{ trans('common.status') }}</label>
                                <div class="layui-input-inline">
                                    <select class="layui-select" name="status" id="status">
                                        @foreach($status as $key=>$value)
                                            <option
                                                value="{{ $key }}" {{ $status_select!="" && $status_select == $key  ? "selected" : "" }}>{{ $value }}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>
                            <div class="layui-form-item layui-inline">
                                <label class="layui-form-label">{{ trans('common.create_time') }}</label>
                                <div class="layui-input-inline">
                                    <input name="create_time" id="create_time"
                                           value="{{ isset($create_time) ? $create_time : "" }}"
                                           placeholder="{{ trans("common.please_input_create_time") }}"
                                           class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item layui-inline">
                                <button class="layui-btn layui-btn-primary"><i class="layui-icon"></i> 搜 索</button>
                                <button class="layui-btn layui-btn-primary" type="button" onclick="resetResult()"><i
                                        class="layui-icon"></i> 重 置
                                </button>
                            </div>
                        </form>
                    </fieldset>
                </caption>

                <thead>
                <tr>
                    <th class="text-left nowrap" width="2%"></th>
                    <th class="text-left nowrap" width="2%">ID</th>
                    <th class="text-left nowrap" width="16%">{{ trans('role.role_name') }}</th>
                    <th class="text-left nowrap" width="20%">{{ trans('role.role_desc') }}</th>
                    <th class="text-left nowrap" width="15%">{{ trans('common.create_time') }}</th>
                    <th class="text-left nowrap" width="5%">{{ trans('common.status') }}</th>
                    <th class="text-left nowrap" width="40%">{{ trans('common.operation') }}</th>
                </tr>
                </thead>
                <tbody>
                @foreach($roles as $role)
                    <tr data-dbclick="">
                        <td class='list-table-check-td think-checkbox'>
                            @if($role->premission != "*")
                                <label><input class="list-check-box" value='{{ $role->id }}' type='checkbox'></label>
                            @endif
                        </td>
                        <td class="text-left nowrap">
                            {{ $role->id }}
                        </td>
                        <td class="text-left nowrap">
                            {{ $role->name }}
                        </td>
                        <td class="text-left nowrap">
                            {{ $role->desc }}
                        </td>
                        <td class="text-left nowrap">
                            @if($role->create_time != 0)
                                {{ date("Y年m月d日 H:i:s",$role->create_time) }}
                            @endif
                        </td>
                        <td class="text-left nowrap">
                            @if($role->status == 0)
                                <span class="layui-badge layui-bg-green">{{ trans('common.enabled') }}</span>
                            @elseif($role->status==1)
                                <span class="layui-badge">{{ trans('common.disabled') }}</span>
                            @endif
                        </td>
                        <td class="text-left nowrap">
                            @if($role->premission != \App\Models\AdminRolesModel::SUPERROLE)
                                @if(array_key_exists('base.role.modify',$btns))
                                    <button class="layui-btn layui-btn-normal layui-btn-xs"
                                            onclick="modifyRole({{ $role->id }})">{{ $btns['base.role.modify'] }}
                                    </button>
                                @endif
                                @if(array_key_exists('base.role.del',$btns))
                                    <button class="layui-btn layui-btn-danger layui-btn-xs"
                                            onclick="deleteRole({{ $role->id }})">
                                        {{ $btns['base.role.del'] }}
                                    </button>
                                @endif
                                @if(array_key_exists('base.role.grant',$btns))
                                    <button class="layui-btn layui-btn-xs"
                                            onclick="grantRole({{ $role->id }})">{{ $btns['base.role.grant'] }}</button>
                                @endif

                                @if(array_key_exists('base.role.disable',$btns) && $role->status == \App\Models\AdminRolesModel::ROLEENABLE)
                                    <button class="layui-btn layui-btn-warm layui-btn-xs"
                                            onclick="disableRole({{ $role->id }})">
                                        {{ $btns['base.role.disable'] }}
                                    </button>
                                @endif
                                @if(array_key_exists('base.role.recovery',$btns) && $role->status == \App\Models\AdminRolesModel::ROLEDISABLE)
                                    <button class="layui-btn layui-btn-normal layui-btn-xs"
                                            onclick="disableRole({{ $role->id }})">
                                        {{ $btns['base.role.recovery'] }}
                                    </button>
                                @endif

                            @endif
                        </td>
                    </tr>
                @endforeach

                </tbody>
            </table>
            <div class="pagination-container nowrap">
                {{ $roles->links() }}
            </div>
        </div>
    </div>

    <script type="text/javascript">
        $(document).ready(function () {
            var laydate = layui.laydate;
            laydate.render({
                elem: '#create_time',
                range: true
            });
        });

        function batchRemove() {
            var ids = "";
            $.each($(".list-check-box"), function (k, v) {
                if ($(this).prop("checked")) {
                    ids += $(this).val() + ","
                }
            });
            if (ids == "") {
                layer.msg("{{ trans('role.please_select_del_role') }}");
                return false;
            }

            var index = layer.confirm("{{ trans('role.confirm_del_role') }}", {
                title: "{{ trans('common.system_info') }}",
                icon: 3,
                anim: 0,
                btn: ["{{ trans('common.sure') }}", "{{ trans('common.cancel') }}"]
            }, function () {
                $.ajax({
                    url: '{{ url('role/del') }}',
                    type: 'POST',
                    dataType: 'json',
                    headers: {
                        'X-CSRF-TOKEN': "{{ csrf_token() }}"
                    },
                    ContentType: "application/json; charset=utf-8",
                    data: {"id": ids},
                    success: function (result) {
                        if (result.code == 0) {
                            layer.msg("{{ trans('common.operation_success') }}", {
                                time: 1000
                            }, function () {
                                window.location.reload();
                            })
                        } else {
                            layer.msg(result.msg)
                        }
                    }
                });
            }, function () {
                layer.close(index)
            });
        }

        function deleteRole(id) {
            var index = layer.confirm("{{ trans('role.confirm_del_role') }}？", {
                title: "{{ trans('common.system_info') }}",
                icon: 3,
                anim: 0,
                btn: ["{{ trans('common.sure') }}", "{{ trans('common.cancel') }}"]
            }, function () {
                $.ajax({
                    url: '{{ url('role/del') }}',
                    type: 'POST',
                    dataType: 'json',
                    headers: {
                        'X-CSRF-TOKEN': "{{ csrf_token() }}"
                    },
                    ContentType: "application/json; charset=utf-8",
                    data: {"id": id},
                    success: function (result) {
                        if (result.code == 0) {
                            layer.msg("{{ trans('common.operation_success') }}", {
                                time: 1000
                            }, function () {
                                window.location.reload();
                            })
                        } else {
                            layer.msg(result.msg)
                        }
                    }
                });
            }, function () {
                layer.close(index)
            });
        }

        function disableRole(id) {
            var index = layer.confirm("{{ trans('role.confirm_disable_role') }}？", {
                title: "{{ trans('common.system_info') }}",
                icon: 3,
                anim: 0,
                btn: ["{{ trans('common.sure') }}", "{{ trans('common.cancel') }}"],
            }, function () {
                $.ajax({
                    url: '{{ url('role/disable') }}',
                    type: 'POST',
                    dataType: 'json',
                    headers: {
                        'X-CSRF-TOKEN': "{{ csrf_token() }}"
                    },
                    ContentType: "application/json; charset=utf-8",
                    data: {"id": id},
                    success: function (result) {
                        if (result.code == 0) {
                            layer.msg("{{ trans('common.operation_success') }}", {
                                time: 1000
                            }, function () {
                                window.location.reload();
                            })
                        } else {
                            layer.msg(result.msg)
                        }
                    }
                });
            }, function () {
                layer.close(index)
            });
        }


        function addRole() {
            layer.open({
                type: 2,
                title: '{{ trans('role.add_role') }}',
                area: ['500px', '320px'],
                content: '{{ url('role/show') }}'
            });
        }

        function grantRole(id) {
            layer.open({
                type: 2,
                title: '{{ trans('role.grant_role') }}',
                area: ['350px', '600px'],
                content: '{{ url('role/grant') }}?id=' + id,
                btn: ["{{ trans('common.sure') }}", "{{ trans('common.cancel') }}"],
                yes: function (index, layero) {
                    var data = {};
                    var premission = layer.getChildFrame('#roleValue', index).val();
                    if (premission == "") {
                        layer.msg("{{ trans('role.please_assign_premission') }}");
                        return false;
                    }
                    data.id = id;
                    data.premission = premission;
                    $.ajax({
                        url: '{{ url('role/dogrant') }}',
                        type: 'POST',
                        dataType: 'json',
                        headers: {
                            'X-CSRF-TOKEN': "{{ csrf_token() }}"
                        },
                        ContentType: "application/json; charset=utf-8",
                        data: data,
                        success: function (result) {
                            if (result.code == 0) {
                                layer.msg("{{ trans('common.operation_success') }}", {
                                    time: 1000
                                }, function () {
                                    window.location.reload();
                                })
                            } else {
                                layer.msg(result.msg)
                            }
                        }
                    });
                },
                cancel: function () {
                    layer.close();
                }
            });
        }


        function modifyRole(id) {
            layer.open({
                type: 2,
                title: '{{ trans('role.modify_role') }}',
                area: ['500px', '300px'],
                content: '{{ url("role/show") }}?id=' + id
            });
        }


        function resetResult() {
            $("#name").val("");
            $("#desc").val("");
            $("#create_time").val("");
            $("#status").val("");
            $("#roleForm").submit();
        }
    </script>
@endsection
